const app = getApp();
const request = require('../../utils/request.js');
const url = require('../../utils/url.js');
const cookie = require('../../utils/cookie.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    login: 0,
    token: '',
    currentVip: {},
    audienceList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token: res.token
          })
          that.getVipList();
        }
      }
    })
  },

  getVipList() {
    request.request(url.getviplist, {}, url.REQUEST_METHOD_POST).then(res => {
      if (res.data.data.data[0]) {
        this.setData({
          currentVip: res.data.data.data[0]
        })
      }
      this.setData({
        audienceList: res.data.data.data,
      })
    })
  },
  setCurrent: function (e) {
    const {
      item: currentVip
    } = e.currentTarget.dataset
    this.setData({
      currentVip
    })
  },
  confirm() {
    var that = this;
    wx.showModal({
      title: '',
      content: `确定要购买${that.data.currentVip.name}吗?`,
      complete: (res) => {
        console.log(res);
        if (res.cancel) {
          wx.showToast({
            title: '支付已取消',
            icon: 'none'
          })
          that.goMine();
          return;
        }

        if (res.confirm) {
          that.payOrder();
        }
      }
    })
  },

  payOrder: function () {
    var that = this;
    let param = {
      scoure: 'wxapp',
      token: that.data.token,
      vip_id: that.data.currentVip.id,
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.createviporder, param, url.REQUEST_METHOD_POST).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if (res.data.code == 1) {
        //直接支付成功
        var config = JSON.parse(res.data.data.wxconfig);
        console.log("获取成功", JSON.parse(res.data.data.wxconfig));
        wx.requestPayment({
          'timeStamp': config.timestamp,
          'nonceStr': config.nonceStr,
          'package': config.package,
          'signType': config.signType,
          'paySign': config.paySign,
          'success': function (res2) {
            console.log("成功", res2)
            if (res2.errMsg == 'requestPayment:ok') {
              wx.showToast({
                title: '支付成功',
                icon: 'success'
              })
              that.goMine();
              return false;
            }
          },
          'fail': function (res2) {
            that.goMine();
            return false;
          },
          'complete': function (res2) {}
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  goMine() {
    wx.redirectTo({
      url: '../user_end/mine/mine',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },
})